package util;

import android.database.Cursor;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/11.
 */
public class TxtDao {
    //判断原来表中是否存在该记录，如果不存在，那么插入新数据
    public static void insertData(String fullPath){
        String sql="select id,full_path,over_flag,now_page from txt where full_path=?";
        Cursor c=Global.util.getReadableDatabase().rawQuery(sql,new String[]{fullPath});
        if (!c.moveToFirst()){
            sql="insert into txt(full_path,over_flag,now_page) values(?,0,1)";
            Global.util.getWritableDatabase().execSQL(sql,new Object[]{fullPath});
        }
        c.close();
    }
    //根据fullpath返回一条记录
    public static Map<String,Object> getDataByFullPath(String fullPath){
        String sql="select id, full_path, now_page, over_flag from txt where full_path=?";
        Cursor c=Global.util.getReadableDatabase().rawQuery(sql,new String[]{fullPath});
        c.moveToFirst();
        Map<String,Object> map=new HashMap<String,Object>();
        while (!c.isAfterLast()) {
            map.put("id", c.getInt(0));
            map.put("fullPath", c.getString(1));
            map.put("nowPage", c.getInt(2));
            map.put("overFlag", c.getInt(3));
            c.moveToNext();
        }
        c.close();
        return map;
    }

    public static void updateTxtByNowPage(int nowPage,String fullPath){
        String sql="updata txt set now_page=? where full_path=?";
        Global.util.getWritableDatabase().execSQL(sql,new Object[]{nowPage,fullPath});
    }

    //分页完成后over_flag变为1
    public static void updataTxtOverFlag(String fullPath){
        String sql="update txt set over_flag=1 where full_path=?";
        Global.util.getWritableDatabase().execSQL(sql,new Object[]{fullPath});
    }
}
